package com.yunji.framework_template.books.biz.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yunji.framework_template.biz.cache.CacheService;
import com.yunji.framework_template.books.orm.persistence.mapper.BooksMapper;
import com.yunji.framework_template.books.orm.persistence.model.Books;
import com.yunji.framework_template.common.web.Page;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by fenglibin on 2019/01/16.
 */
@Slf4j
@Service
public class BooksService {

    @Resource
    private BooksMapper  booksMapper;
    @Resource
    private CacheService cacheService;

    public int deleteByPrimaryKey(Integer id) {
        return booksMapper.deleteByPrimaryKey(id);
    }

    public int insert(Books record) {
        return booksMapper.insert(record);
    }

    public int insertSelective(Books record) {
        return booksMapper.insertSelective(record);
    }

    /**
     * 先从缓存中获取，取不到再从数据库中获取
     * 
     * @param id
     * @return
     */
    public Books selectByPrimaryKey(Integer id) {
        Books book = cacheService.getCountryCache().getBooks().get(id);
        if (book != null) {
            return book;
        }
        return booksMapper.selectByPrimaryKey(id);
    }
    
    public Books selectByBookId(Integer bookId) {
        return booksMapper.selectByBookId(bookId);
    }

    public int updateByPrimaryKeySelective(Books record) {
        return booksMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKeyWithBLOBs(Books record) {
        return booksMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    public int updateByPrimaryKey(Books record) {
        return booksMapper.updateByPrimaryKey(record);
    }

    public List<Books> selectByCondition(Books record) {
        return booksMapper.selectByCondition(record);
    }

    public List<Books> selectAll() {
        return booksMapper.selectAll();
    }

    public List<Books> selectByPage(Page<Books> page) {
        return booksMapper.selectByPage(page);
    }

    /**
     * 根据是否传入了Cover判断是否获取有Cover或没有Cover的电子书
     * 
     * @param page
     * @return
     */
    public List<Books> selectByCoverByPage(Page<Books> page) {
        return booksMapper.selectByCoverByPage(page);
    }

    /**
     * 实时获取当前页，和selectByPage的区别在于其会重新统计总记录数
     * 
     * @param currentPage
     * @return
     */
    public List<Books> selectByRealTimePage(int currentPage) {
        int totalCount = count(new Books());
        return selectByPage(-1, totalCount, currentPage);
    }

    public List<Books> selectByRealTimePage(int pageSize, int currentPage) {
        int totalCount = count(new Books());
        return selectByPage(pageSize, totalCount, currentPage);
    }

    public List<Books> selectByPage(int totalCount, int currentPage) {
        return selectByPage(-1, totalCount, currentPage);
    }

    public List<Books> selectByPage(int pageSize, int totalCount, int currentPage) {
        Page<Books> page = new Page<Books>();
        if (pageSize > 0) {
            page.setPageSize(pageSize);
        }
        log.info("init books,current page:" + currentPage);
        page = page.sequentialDisplayCalculate(currentPage, totalCount);
        if (page.getTotalCount() == 0 || page.getTotalPages() == 0) {
            return null;
        }
        return selectByPage(page);
    }

    public Integer count(Books record) {
        return booksMapper.count(record);
    }

    public Integer countByCover(Books record) {
        return booksMapper.countByCover(record);
    }

    public int deleteByCondition(Books record) {
        return booksMapper.deleteByCondition(record);
    }

    /**
     * 随机取最多10本书
     * 
     * @return
     */
    public List<Books> getRandomBooks() {
        List<Books> booksList = new ArrayList<Books>();
        Set<Integer> seeds = new HashSet<Integer>();
        List<Integer> booksIdList = cacheService.getCountryCache().getBooksIdList();
        for (int i = 0; i < 10; i++) {
            int randomIndex = (int) (Math.random() * booksIdList.size());
            if (!seeds.contains(randomIndex)) {
                booksList.add(cacheService.getCountryCache().getBooks().get(booksIdList.get(randomIndex)));
                seeds.add(randomIndex);
            }
        }
        return booksList;
    }

    public int updateReadTimesByPrimaryKey(Books book) {
        return booksMapper.updateReadTimesByPrimaryKey(book);
    }

    /**
     * 指定页号，获取指定页书的列表
     * 
     * @param currentPage
     * @return
     */
    public List<Books> getBookByPageIndex(int currentPage) {
        List<Books> bookList = cacheService.getCountryCache().getPageBook().get(currentPage);
        if (bookList == null) {
            int pageSize = 10;
            int totalCount = cacheService.getCountryCache().getTotalBooks();

            Page<Books> page = new Page<Books>();
            if (pageSize > 0) {
                page.setPageSize(pageSize);
            }
            log.info("Get books,current page:" + currentPage);
            page = page.sequentialDisplayCalculate(currentPage, totalCount);
            if (page.getTotalCount() == 0 || page.getTotalPages() == 0) {
                return null;
            }
            Books book = new Books();
            book.setCover("Cover is not null");
            page.setObj(book);
            bookList = selectByCoverByPage(page);
        }
        return bookList;
    }

}
